﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace zaklad_rtv
{
    /// <summary>
    /// Interaction logic for ClientList.xaml
    /// </summary>
    public partial class ClientList
    {
        public ClientList()
        {
            InitializeComponent();
        }

        private void ConfirmDelete(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show("Czy na pewno chcesz usunąć tego klienta z bazy danych? Wszystkie naprawy, do których ten klient był przypisany, nie będą już miały przypisanego klienta.", "Usuwanie klienta", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result.Equals(MessageBoxResult.Yes))
            {
                var clientToDelete = _listView.SelectedItem as Client;
                if (clientToDelete != null)
                {
                    ObjectCollection.Remove(clientToDelete);
                    MainWindow.DBHelper.Delete(clientToDelete);
                    RaiseEvent(new DatabaseObjectChangedEventArgs(AppEvents.DatabaseObjectChangedEvent, sender, clientToDelete, EventAction.Delete));
                    RefreshList();
                }
            }
        }

        protected override void RefreshList()
        {
            _listView.ItemsSource = null;
            _listView.ItemsSource =
                from c in ObjectCollection
                orderby c.LastName, c.FirstName
                select c;
            foreach (var column in ((GridView)_listView.View).Columns)
            {
                column.Width = column.ActualWidth;
                column.Width = double.NaN;
            }
        }

        protected override ICriteria<Client> ComposeCriteria()
        {
            throw new System.NotImplementedException();
        }

        protected override void ViewFilteredList(ICriteria<Client> criteria)
        {
            throw new System.NotImplementedException();
        }

        private void GoToEdit(object sender, RoutedEventArgs e)
        {
            ((ClientForm)MainWindow._clientFormTab.Content).EditObject = _listView.SelectedItem as Client;
            MainWindow.ChangeActiveTab(MainWindow.Tabs.ClientForm);
        }

        private void GoToNew(object sender, RoutedEventArgs e)
        {
            _listView.SelectedItem = null;
            ((ClientForm)MainWindow._clientFormTab.Content).EditObject = null;
            MainWindow.ChangeActiveTab(MainWindow.Tabs.ClientForm);
        }
        private void GoToFilteredServiceList(object sender, RoutedEventArgs e)
        {
            var selectedClient = _listView.SelectedItem as Client;
            if (selectedClient != null)
            {
                var serviceList = ((ServiceList)MainWindow._serviceListTab.Content);
                serviceList._filterClient.Text = selectedClient.FullString;
                serviceList.DoFilter(null, null);
                MainWindow.ChangeActiveTab(MainWindow.Tabs.ServiceList);
            }
        }

        public void ClientAdded(Client client)
        {
            ObjectCollection.Add(client);
            RefreshList();
        }

        public void ClientEdited(Client client)
        {
            RefreshList();
        }

        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_listView.SelectedItem != null)
            {
                ((ClientForm)MainWindow._clientFormTab.Content).EditObject = _listView.SelectedItem as Client;
            }
            else
            {
                ((ClientForm)MainWindow._clientFormTab.Content).EditObject = null;
            }
        }
    }
}
